package com.fafa.tankgame04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * 坦克大战的绘图区域
 * 面板
 */
public class MyPanel extends JPanel implements KeyListener, Runnable {

    //定义我的坦克
    Hero hero = null;
    Vector<EnemyTank> enemyTanks = new Vector<>();

    //当子弹击中坦克加入
    Vector<Bomb> bombs = new Vector<>();

    Image image1 = null;
    Image image2 = null;
    Image image3 = null;
    int enemyTankCount = 3;

    public MyPanel() {
        hero = new Hero(100, 100);

        for (int i = 0; i < enemyTankCount; i++) {
            EnemyTank enemyTank = new EnemyTank(50 + 50 * i, 0);
            enemyTank.setDirect(0);


            new Thread(enemyTank).start();
            enemyTanks.add(enemyTank);


        }
        //创建一个对象让图片加载一下
        bombs.add(new Bomb(-100, -100));
        //初始化图片

        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/com/fafa/tankgame04/Image/1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/com/fafa/tankgame04/Image/2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/com/fafa/tankgame04/Image/3.gif"));

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //填充矩形，默认黑色
        g.fillRect(0, 0, 1000, 750);

        //画图坦克方法
        if (hero.isLive)
            drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0);
        //画出hero射出的子弹
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);
            if (shot != null && shot.isLive) {
                g.setColor(Color.red);
                g.fill3DRect(shot.x, shot.y, 10, 10, false);
            }
            if (shot != null && !shot.isLive) {
                hero.shots.remove(shot);
                //用迭代器遍历删除会出问题，原因在于迭代器调用的是vector的父类的迭代器，删除后比较两者的size会出问题
            }
        }
        //敌方坦克和子弹
        if (!enemyTanks.isEmpty()) {
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                if (enemyTank.isLive) { //存在才画
                    drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 1);

                    for (int j = 0; j < enemyTank.shots.size(); j++) {
                        Shot shot1 = enemyTank.shots.get(j);
                        if (shot1.isLive) {
                            g.fill3DRect(shot1.x, shot1.y, 10, 10, false);
                        } else {
                            enemyTank.shots.remove(shot1);
                        }
                    }
                } else {
                    enemyTanks.remove(enemyTank);
                }

            }
        }


        //画图炸弹
        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            if (bomb.life > 6) {
                g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
            } else if (bomb.life > 3) {
                g.drawImage(image2, bomb.x, bomb.y, 60, 60, this);
            } else {
                g.drawImage(image3, bomb.x, bomb.y, 60, 60, this);
            }

            //炸弹生命值减少
            bomb.lifeDown();
            if (!bomb.isLive) {
                bombs.remove(bomb);
            }
        }

    }


    /**
     * @param x      坦克的x坐标
     * @param y      坦克的y坐标
     * @param g      画笔
     * @param direct 方向
     * @param type   坦克类型
     */
    public void drawTank(int x, int y, Graphics g, int direct, int type) {

        //根据颜色
        switch (type) {
            case 0:
                g.setColor(Color.cyan);//青色
                break;
            case 1:
                g.setColor(Color.yellow);//黄色
                break;
        }


        //根据方向
        switch (direct) {
            case 0: //向上 左右轮子 盖子 炮管
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y, x + 20, y + 30);
                break;
            case 1: //向下 左右轮子 盖子 炮管
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 40, x + 20, y + 60);
                break;
            case 2:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x, y + 20, x + 30, y + 20);
                break;
            case 3:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 60, y + 20, x + 30, y + 20);
                break;
            default:
                System.out.println("暂无");
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int speed = hero.getSpeed();
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {
            hero.moveUp();
            hero.setDirect(0);
        } else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) {
            hero.moveDown();
            hero.setDirect(1);
        } else if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
            hero.moveLeft();
            hero.setDirect(2);
        } else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
            hero.moveRight();
            hero.setDirect(3);
        } else if (keyCode == KeyEvent.VK_J) {
            //System.out.println("按下了J");
            hero.shotEnemyTank();
        } else if (keyCode == KeyEvent.VK_G) {
            System.out.println(hero.shots.size());
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            if (!hero.isLive) {
                System.out.println("你被击中了,游戏结束");
                break;
            }
            //重绘
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //判断坦克被击中了敌人坦克
            for (int i = 0; i < hero.shots.size(); i++) {
                //遍历子弹
                Shot shot = hero.shots.get(i);
                if (shot.isLive) {
                    //遍历所有坦克
                    for (int j = 0; j < enemyTanks.size(); j++) {
                        EnemyTank enemyTank = enemyTanks.get(j);
                        hitTank(shot, enemyTank);
                    }
                }
            }
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                if (enemyTank.isLive) {
                    for (int j = 0; j < enemyTank.shots.size(); j++) {
                        Shot shot = enemyTank.shots.get(j);
                        if (shot.isLive) {
                            hitTank(shot, hero);

                        }
                    }
                }
            }


            this.repaint();

        }
    }


    //编写方法判断子弹是否碰到坦克
    public void hitTank(Shot s, Tank Tank) {
        if (Tank.isLive) {
            if (Tank.getDirect() == 0 || Tank.getDirect() == 2) {
                //边界触碰
                if (s.x > Tank.getX() && s.x < Tank.getX() + 40
                        && s.y > Tank.getY() && s.y < Tank.getY() + 60) {
                    s.isLive = false;
                    Tank.isLive = false;
                    //创建Bomb对象，加入集和
                    Bomb bomb = new Bomb(Tank.getX(), Tank.getY());
                    bombs.add(bomb);
                }
            }
            if (Tank.getDirect() == 1 || Tank.getDirect() == 3) {
                //触碰
                if (s.x > Tank.getX() && s.x < Tank.getX() + 60
                        && s.y > Tank.getY() && s.y < Tank.getY() + 40) {
                    s.isLive = false;
                    Tank.isLive = false;
                    //创建Bomb对象，加入集和
                    Bomb bomb = new Bomb(Tank.getX(), Tank.getY());
                    bombs.add(bomb);
                }
            }
        }


    }
}


